/*
 *  Copyright 2010 UPF Telecos 4th group G.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.database.impl;

import edu.upf.core.ClientLogic;
import edu.upf.database.DBAction;
import edu.upf.database.DBActionResult;
import edu.upf.database.DBManager;
import edu.upf.messages.SearchResult;
import edu.upf.utils.Definitions;
import edu.upf.exceptions.DBConnectionFailed;
import java.io.IOException;

/**
 * <p>Encapsulates the database acces by providing methods for executing the
 * different queries</p>
 * @author netjois
 */
public class MediaDAO implements Definitions {

    private DBManager db;
    private DBAction action;
    private DBActionResult dbResult;
    private SearchResult searchResult;
    private ClientLogic clientLogic;

    /**
     * Constructor, initializes the database access. Receives the ClientLogic
     * object for callback purposes
     * @param clientLogic
     */
    public MediaDAO(ClientLogic clientLogic) {
        try{
            db = DBManagerImpl.getInstance();
            this.clientLogic=clientLogic;

        }catch(DBConnectionFailed ex){
            /*Por el momento tratamos igual todas las excepciones, más adelante
             si es necesario se pueden distinguir en diferentes catch segun el
             nivel de gravedad o tratamiento necesario*/
            clientLogic.showExceptionMessage(ex);
        } catch (IOException ex){
            clientLogic.showExceptionMessage(ex);
        }
    }

    /**
     * Performs a keyword search for image and video
     * @param queryParam
     * @return
     */
    public SearchResult basicAllSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_IMAGE_BASIC,queryParam);
        genericQuery(QUERY_VIDEO_BASIC,queryParam);
        return searchResult;
    }
    
    /**
     * Performs a keyword search for image
     * @param queryParam
     * @return
     */
    public SearchResult basicImageSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_IMAGE_BASIC,queryParam);
        return searchResult;
    }


    /**
     * Performs a keyword search for video
     * @param queryParam
     * @return
     */
    public SearchResult basicVideoSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_VIDEO_BASIC,queryParam);
        return searchResult;
    }

    /**
     * Performs a category search for image and video
     * @param queryParam
     * @return
     */
    public SearchResult categorySearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        if(queryParam.length==1){
            genericQuery(QUERY_IMAGE_CATEGORY,queryParam);
            genericQuery(QUERY_IMAGE_CATEGORY,queryParam);
        }else if(queryParam.length==2){
            genericQuery(QUERY_IMAGE_CATEGORYPLUS,queryParam);
            genericQuery(QUERY_IMAGE_CATEGORYPLUS,queryParam);
        }
        return searchResult;
    }

    /**
     * Performs an article search for image and video
     * @param queryParam
     * @return
     */
    public SearchResult articleSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_IMAGE_ARTICLE,queryParam);
        genericQuery(QUERY_VIDEO_ARTICLE,queryParam);
        return searchResult;
    }

    /**
     * Performs a title search for image and video
     * @param queryParam
     * @return
     */
    public SearchResult titleSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_IMAGE_TITLE,queryParam);
        genericQuery(QUERY_VIDEO_TITLE,queryParam);
        return searchResult;
    }

    /**
     * Performs a paragraph search for image and video
     * @param queryParam
     * @return
     */
    public SearchResult paragraphSearch(String[] queryParam){
        searchResult=new SearchResult(queryParam[FREE_TEXT_POS]);
        genericQuery(QUERY_IMAGE_PARAGRAPH,queryParam);
        genericQuery(QUERY_VIDEO_PARAGRAPH,queryParam);
        return searchResult;
    }

    //Performs a query with the given parameters
    private void genericQuery(String query, String[] queryParam){
        
        action = new DBActionImpl(query, queryParam);
        dbResult = (DBActionResult) db.execute(action);

        if(dbResult.isOk()){
            if(searchResult.getList()==null){
                searchResult.setList(dbResult.getResult());
            }else{
                searchResult.addList(dbResult.getResult());
            }
        }else{
            clientLogic.showExceptionMessage(dbResult.getException());
        }
    }
}
